<!-- register.php -->
<?php
// 连接数据库
$host = "localhost";
$dbname = "db";
$username = "root";
$password = "";
$conn = new mysqli($host, $username, $password, $dbname);

if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];

// 验证用户名和邮箱是否已存在
$sql = "SELECT * FROM users WHERE username='$username' OR email='$email'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "错误：用户名或邮箱已存在";
} else {
    // 插入新用户
    $sql = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";
    if ($conn->query($sql) === TRUE) {
        $targetUrl = "zhuanye.html";
        header("Location: " . $targetUrl);
        exit();
    } else {
        echo "注册失败：" . $conn->error;
    }
}

$conn->close();
?>
